#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "json.h"
// 跳过空白字符
static void skip_whitespace(const char** ptr) {
    while (isspace(**ptr)) {
        (*ptr)++;
    }
}

// 解析数值并存储到目标缓冲区
static void parse_number(const char** ptr, char* target) {
    int value = 0;
    while (isdigit(**ptr)) {
        value = value * 10 + (**ptr - '0');
        (*ptr)++;
    }
    snprintf(target, MAX_FIELD_LEN, "%d", value);
}

// 主解析函数
void parse_control_state(const char* json, ControlState* state) {
    const char* p = json;

    // 定位到paras对象
    p = strstr(p, "\"paras\"");
    if (!p) return;
    p += 6;  // 跳过"paras"

    // 定位到对象起始位置
    skip_whitespace(&p);
    if (*p++ != ':') return;
    skip_whitespace(&p);
    if (*p++ != '{') return;

    while (*p && *p != '}') {
        skip_whitespace(&p);
        if (*p != '"') break;
        p++;  // 跳过引号

        // 提取键名
        const char* key_start = p;
        while (*p != '"') p++;
        char key[32] = {0};
        strncpy(key, key_start, p - key_start);
        p++;  // 跳过结尾引号

        // 定位到值
        skip_whitespace(&p);
        if (*p++ != ':') break;
        skip_whitespace(&p);

        // 根据键名处理值
        if (strcmp(key, "gather") == 0) {
            printf("gather\r\n");
            if (strncmp(p, "true", 4) == 0) {
                snprintf(state->switchFlag, MAX_FIELD_LEN, "1");
                p += 4;
                printf("1\r\n");
            } else if (strncmp(p, "false", 5) == 0) {
                snprintf(state->switchFlag, MAX_FIELD_LEN, "0");
                p += 5;
                printf("0\r\n");
            }
        } else if (strcmp(key, "open_fan") == 0) {
            if (strncmp(p, "true", 4) == 0) {
                snprintf(state->fanFlag, MAX_FIELD_LEN, "1");
                p += 4;
            } else if (strncmp(p, "false", 5) == 0) {
                snprintf(state->fanFlag, MAX_FIELD_LEN, "0");
                p += 5;
            }
        } else if (strcmp(key, "open_water") == 0) {
            if (strncmp(p, "true", 4) == 0) {
                snprintf(state->waterFlag, MAX_FIELD_LEN, "1");
                p += 4;
            } else if (strncmp(p, "false", 5) == 0) {
                snprintf(state->waterFlag, MAX_FIELD_LEN, "0");
                p += 5;
            }
        } else if (strcmp(key, "open_led") == 0) {
            if (strncmp(p, "true", 4) == 0) {
                snprintf(state->sunFlag, MAX_FIELD_LEN, "1");
                p += 4;
            } else if (strncmp(p, "false", 5) == 0) {
                snprintf(state->sunFlag, MAX_FIELD_LEN, "0");
                p += 5;
            }
        } else if (strcmp(key, "led_red") == 0) {
            parse_number(&p, state->colorNum[0]);
        } else if (strcmp(key, "led_blue") == 0) {
            parse_number(&p, state->colorNum[1]);
        } else if (strcmp(key, "led_green") == 0) {
            parse_number(&p, state->colorNum[2]);
        } else if (strcmp(key, "id") == 0) {
            parse_number(&p, state->id);
        }

        // 跳过逗号/空格
        skip_whitespace(&p);
        if (*p == ',') p++;
    }
}

// // 示例使用
// int main() {
//     const char* json = "{\"paras\":{\"gather\":true,\"open_fan\":true,\"open_water\":true,\"open_led\":true,\"led_red\":1,\"led_blue\":11,\"led_green\":111,\"id\":1}}";
//     ControlState state = {0};
    
//     parse_control_state(json, &state);
    
//     printf("ID: %s\n", state.id);
//     printf("Fan: %s\n", state.fanFlag);
//     printf("Water: %s\n", state.waterFlag);
//     printf("LED: %s\n", state.sunFlag);
//     printf("Switch: %s\n", state.switchFlag);
//     printf("RGB: [%s, %s, %s]\n", 
//            state.colorNum[0], 
//            state.colorNum[1], 
//            state.colorNum[2]);
    
//     return 0;
// }